﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Kugar.Core.Configuration;
using Kugar.DC.Car.Web.Areas.Wechat.Helpers;
using Kugar.Web.Wechat.Authentications;
using Kugar.Web.Wechat.Entities;
using Kugar.Web.Wechat.Enums;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Kugar.Web.Wechat.NetCore.Test
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            //services.UseWechatOAuth(Configuration);
            //services.UseWechatPay(); //微支付必须

            //var t = new WechatAuthenticationOptions();
            services.UseWechatOAuth(configuration: Configuration);
            services.AddSession();

            services.AddAuthentication("sss")
                .AddWechatCookie("wechat",
                    new MPOAuthHandler(),
                    defaultUser: new WechatUserOAuth()
                    {
                        OpenID = "sdsfdfsf",
                        NickName = "test",
                        Scope = WechatOAuthScope.snsapi_base
                    }, defaultUserAppID: "wxb9d5d350038db8aa");
            ;

            WechatApi.Default.Pay.RequestPayKey += RequestPayKey;

            //services.AddAuthentication()
            //    .AddCookie("wechat", "wechat", x =>
            //    {
            //        x.Events.OnValidatePrincipal = async (context) =>
            //        {
            //            var p = context.Principal;
            //        };
            //    });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }

        public static async Task<string> RequestPayKey(string appID, string mchID)
        {
            return CustomConfigManager.Default["Wechat:Pay:PayKey"];
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseSession();

            app.UseAuthentication();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
